﻿using System;
namespace Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="paringliht_v3_vastus", Namespace="http://producers.arireg.xtee.riik.ee/producer/arireg", Target=typeof(ParinglihtV3Vastus))]
    public class ParinglihtV3VastusSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected IParinglihtV3Vastus clazz;
        
        public ParinglihtV3VastusSerializer(IParinglihtV3Vastus clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("ettevotjad");
            if (clazz.Ettevotjad!=null) {
                Writer.WriteTypeAttribute("http://schemas.xmlsoap.org/soap/encoding/", "Array");
                Writer.WriteArrayTypeAttribute("paringliht_v3_ettevote", "http://producers.arireg.xtee.riik.ee/producer/arireg", clazz.Ettevotjad.Count);
                for (int x = 0; ((clazz.Ettevotjad != null) 
                            && (x < clazz.Ettevotjad.Count)); x = (x + 1)) {
                    if (clazz.Ettevotjad[x]!=null) {
                        Writer.WriteStartElement("item");
                        global::Xtee.Core.SerializationMetadata metaEttevotjad = this.GetMetaData(clazz.Ettevotjad[x]);
                        Writer.WriteTypeAttribute(metaEttevotjad.TypeQualifiedName.Namespace, metaEttevotjad.TypeQualifiedName.Name);
                        metaEttevotjad.NewSerializer(clazz.Ettevotjad[x]).WriteXml(Writer);
                        Writer.WriteEndElement();
                    }
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Ettevotjad");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("leitud_ettevotjate_arv");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "integer");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdInteger(((decimal)(clazz.LeitudEttevotjateArv))).ToString());
            Writer.WriteEndElement();
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="ettevotjad") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>paringliht_v3_vastus>ettevotjad");
                }
                clazz.Ettevotjad = new System.Collections.Generic.List<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IParinglihtV3Ettevote>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("ettevotjad");
                    for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                && (Reader.LocalName == "item")); ) {
                        if (Reader.IsElementNil()) {
                            throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>paringliht_v3_ettevotted>item");
                        }
                        global::Xtee.Core.SerializationMetadata metaEttevotjad = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IParinglihtV3Ettevote>(Reader, "paringliht_v3_ettevote", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                        global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IParinglihtV3Ettevote item = metaEttevotjad.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IParinglihtV3Ettevote>();
                        clazz.Ettevotjad.Add(item);
                        if ((Reader.IsEmptyElement == false)) {
                            Reader.ReadStartElement("item");
                            metaEttevotjad.NewSerializer(item).ReadXml(Reader);
                            Reader.ReadEndElement();
                        }
                        else {
                            Reader.Skip();
                        }
                    }
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>paringliht_v3_vastus>ettevotjad", Reader.LocalName);
            }
            if (Reader.LocalName=="leitud_ettevotjate_arv") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>paringliht_v3_vastus>leitud_ettevotjate_arv");
                }
                clazz.LeitudEttevotjateArv = new global::Xtee.Core.Schema.XsdInteger(Reader.ReadElementString("leitud_ettevotjate_arv")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>paringliht_v3_vastus>leitud_ettevotjate_arv", Reader.LocalName);
            }
        }
    }
}
